home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 2: CDPD 1
/
Almathera Ten on Ten - Disc 2: CDPD 1.iso
/
pd
/
101-125
/
103
/
sol
/
solx.c
< prev
Wrap
C/C++ Source or Header
|
1995-03-13
|
14KB
|
690 lines
/* solx.c - solitare
ported to the amiga from unix 4.3bsd
by Joel Swank 9/22/87
*/
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <exec/types.h>
#include <stat.h>
#include <libraries/dos.h>
#include <functions.h>
#undef TRUE
#undef FALSE
#define TRUE 1
#define FALSE 0
int amode;
int cnt;
int cheat;
int deck[53];
int ace[4][14];
int run[7][53];
int hidden[7][8];
char cmd[40], *cp;
char *resgb;
int long_run[7];
int long_hide[7];
int long_ace[4];
int base_run[7];
extern struct tm *localtime();
extern char *getenv();
extern long time();
main(n, a)
char **a;
{
int i, p, c, r, s;
long now;
struct tm *tp;
int from;
int split;
int dest;
int brkflg;
amode = 0;
(void) time(&now);
tp = localtime(&now);
srand((unsigned) (tp->tm_sec));
init();
for (i = 0; i < 52; i++)
deck[i] = i;
deck[52] = -1;
shuffle(deck);
for (i = 0; i < 4; i++)
ace[i][0] = -1;
for (i = 0; i < 7; i++)
hidden[i][0] = run[i][0] = -1;
for (p = 0; p < 7; p++)
for (c = p; c < 7; c++)
put(hidden[c],get(deck));
put(run[0], get(hidden[0]));
for (r = 1; r < 7; r++)
for (i = 0; i < 4; i++)
put(run[r],get(deck));
redraw(TRUE);
for (r = 0; r < 7; r++)
{
while (getvalue(peek(run[r])) == 0)
{
s = getsuit(peek(run[r]));
put(ace[s],get(run[r]));
if (run[r][0] == -1)
put(run[r],get(hidden[r]));
redraw(FALSE);
}
}
cnt = cheat = 0;
if (n > 1 && !strcmp(a[1], "auto"))
autopilot();
brkflg = 1;
while (brkflg)
{
cnt++;
do
{
moveto(19, 0);
(void) printf("cmd:%d> ",cnt);
cleol();
(void) fflush(stdout);
} while (!getst(cp = cmd));
while (*cp == ' ' || *cp == '\t')
cp++;
switch (*cp++)
{
case 'a':
autopilot();
break;
case 'm':
{
while (*cp == ' ' || *cp == '\t')
cp++;
if (!(from = *cp++) || from < '1' || from > '7')
{
whoops();
continue;
}
while (*cp == ' ' || *cp == '\t')
cp++;
for (split = 0; isdigit(*cp); split = split * 10 + *cp++ - '0')
;
if (split < 1 || split > lstlen(run[from - '1']))
{
whoops();
continue;
}
while (*cp == ' ' || *cp == '\t')
cp++;
if (!(dest = *cp++) || ((dest < '1' || dest > '7') &&
dest != 'a') || !movecard(from, split, dest, FALSE))
{
whoops();
continue;
}
if (ordered())
finish();
}
break;
case 'h':
case '?':
disphelp();
break;
case 'r':
disprules();
break;
case 'q':
brkflg = 0;
break;
case 'w':
{
while (*cp == ' ' || *cp == '\t')
cp++;
if ((from = *cp - '1') < 0 || from > 6)
{
whoops();
continue;
}
moveto(21, 0);
(void) printf("%d cards hidden under run %c:\n", lstlen(hidden[from]),
from + '1');
for (i = 0; hidden[from][i] != -1; i++)
{
pcard(hidden[from][i]);
(void) putchar(' ');
}
(void) printf("\nHit return -");
(void) fflush(stdout);
(void) getchar();
moveto(21, 0);
cleol();
(void) putchar('\n');
cleol();
cheat++;
}
break;
case 'p':
{
while (*cp == ' ' || *cp == '\t')
cp++;
if ((from = *cp - '1') < 0 || from > 6)
{
whoops();
continue;
}
while (hidden[from][0] != -1)
put(run[from], get(hidden[from]));
redraw(FALSE);
cheat++;
}
break;
case 's':
{
while (*cp == ' ' || *cp == '\t')
cp++;
if ((from = *cp - '1') < 0 || from > 6)
{
whoops();
continue;
}
shuffle(run[from]);
long_run[from] = -1;
redraw(FALSE);
cheat++;
}
break;
default:
whoops();
break;
}
}
moveto(21, 0);
cleol();
(void) printf("I see you gave up\n");
if (cheat > 0)
(void) printf(" ... even after you cheated %d times!\n", cheat);
else
(void) printf(" ... but at least you didn't cheat...congratulations!\n");
unwind(0);
}
unwind(ret)
{
moveto(23, 0);
deinit();
exit(ret);
}
movecard(from, split, dest, limitmove)
{
if (from == dest)
return(FALSE);
return(dest == 'a' ? run2ace(from) : run2run(from, split, dest, limitmove));
}
run2ace(from)
{
int fcard, s, ok;
fcard = peek(run[from -= '1']);
if (ok = getvalue(peek(ace[s = getsuit(fcard)])) == getvalue(fcard) - 1)
{
put(ace[s], get(run[from]));
redraw(FALSE);
tidy(from);
}
return(ok);
}
run2run(from, split, dest, limitmove)
{
int fcard, dcard, i, ok;
if ((fcard = run[from -= '1'][--split]) == -1)
return(FALSE);
dcard = peek(run[dest -= '1']);
if (amode > 0 && dcard == -1 && getvalue(fcard) == 12 &&
lstlen(hidden[from]) == 0)
return(FALSE);
if (amode > 0 && limitmove && split && chk2run(fcard, run[from][split - 1]))
return(FALSE);
if (ok = chk2run(fcard, dcard))
{
for (i = split; run[from][i] != -1; i++)
put(run[dest], run[from][i]);
run[from][split] = -1;
redraw(FALSE);
tidy(from);
}
return(ok);
}
tidy(from)
{
int s;
while (getvalue(peek(run[from])) == 0)
{
s = getsuit(peek(run[from]));
put(ace[s],get(run[from]));
redraw(FALSE);
}
if (lstlen(hidden[from]) != 0 && lstlen(run[from]) == 0)
{
while (getvalue(peek(hidden[from])) == 0)
{
s = getsuit(peek(hidden[from]));
put(ace[s],get(hidden[from]));
redraw(FALSE);
}
put(run[from],get(hidden[from]));
redraw(FALSE);
}
}
chk2run(fcard,dcard)
{
if (dcard == -1 && getvalue(fcard) == 12)
return(TRUE);
return(getvalue(dcard) - 1 == getvalue(fcard) &&
getcolour(dcard) != getcolour(fcard));
}
ordered()
{
int i, j;
for (i = 0; i < 7; i++)
if (hidden[i][0] != -1)
return(FALSE);
else if (run[i][0] != -1)
for (j = 1; run[i][j] != -1; j++)
if (getvalue(run[i][j]) != getvalue(run[i][j - 1]) - 1 ||
getcolour(run[i][j]) == getcolour(run[i][j - 1]))
return(FALSE);
return(TRUE);
}
cardsleft()
{
int i;
for (i = 0; i < 7; i++)
if (run[i][0] != -1)
return(TRUE);
return(FALSE);
}
finish()
{
int i;
moveto(21, 0);
(void) printf("\007I'll finish for you now\007");
(void) fflush(stdout);
do
for (i = 0; i < 7; i++)
while (movecard(i + '1', 0, 'a', FALSE))
;
while (cardsleft());
moveto(21, 40);
cleol();
(void) printf("\007You WIN\007\n");
if (cheat > 0)
(void) printf(" ... but you cheated %d times!", cheat);
else
(void) printf(" ... and without cheating ... congratulations!");
unwind(0);
}
autopilot()
{
int i, j, k, l, ret;
moveto(21, 0);
(void) printf("Going into automatic mode...");
(void) fflush(stdout);
amode = TRUE;
do
{
l = 0;
for (i = 0; i < 7; i++)
for (j = 0; j < 7; j++)
for (k = 0; k < lstlen(run[i]); k++)
l = movecard(i + '1', k+1, j + '1', TRUE) || l;
for (i = 0; i < 7; i++)
l = movecard(i + '1', 0, 'a', FALSE) || l;
} while (l);
moveto(21, 28);
if (!cardsleft())
{
(void) printf("\007YEA...\007");
(void) fflush(stdout);
for (i = 0; i < 7; i++)
if (movecard(i + '1', 0, 'a', FALSE))
moveto(21, 34);
(void) printf("I won!!!!!");
ret=0;
}
else
{
(void) printf("I couldn't win this time");
ret=1;
}
unwind(ret);
}
redraw(display)
{
int i, j;
if (display)
{
clear();
for (i = 0; i < 7; i++)
{
long_run[i] = long_hide[i] = base_run[i] = -1;
moveto(i + i + 2, 1);
(void) putchar(i + '1');
}
moveto(0, 6);
for (i = 1; i < 21; i++)
(void) printf("%3d", i);
for (i = 0; i < 4; i++)
long_ace[i] = -1;
moveto(17, 1);
(void) printf("ACES:");
}
for (i = 0; i < 7; i++)
{
if (long_hide[i] != lstlen(hidden[i]))
{
moveto(i + i + 2, 4);
(void) putchar(lstlen(hidden[i]) ? lstlen(hidden[i]) + '0' : ' ');
long_hide[i] = lstlen(hidden[i]);
}
}
for (i = 0; i < 4; i++)
{
if (long_ace[i] != lstlen(ace[i]))
{
moveto(17, 8 + i * 5);
if (ace[i][0] != -1)
pcard(peek(ace[i]));
else
(void) printf("--");
long_ace[i] = lstlen(ace[i]);
}
}
for (i = 0; i < 7; i++)
{
if (long_run[i] != lstlen(run[i]) ||
(long_run[i] == 1 && base_run[i] != run[i][0]))
{
moveto(i + i + 2, 7);
for (j = 0; run[i][j] != -1; j++)
{
if (run[i][j] == -1)
(void) printf(" ");
else
pcard(run[i][j]);
(void) putchar(' ');
}
while (j++ < long_run[i])
(void) printf(" ");
long_run[i] = lstlen(run[i]);
base_run[i] = run[i][0];
}
}
(void) fflush(stdout);
(void) Delay(10L);
}
pcard(card)
{
int i;
if ((i = getcolour(card)) != 0)
standout();
(void) printf("%c%c", "A23456789TJQK"[getvalue(card)], "HDSC"[getsuit(card)]);
if (i)
standend();
}
getvalue(card)
{
return(card % 13);
}
getsuit(card)
{
return(card / 13);
}
getcolour(card)
{
return(card / 26);
}
whoops()
{
moveto(19, 0);
(void) printf("\007Invalid Command: '%s'\007", cmd);
(void) fflush(stdout);
(void) Delay(40L);
}
disphelp()
{
clear();
(void) printf("\
Commands: m [1-7] [pos] [1-7a] : move cards or runs\n\
a : turn on the auto pilot\n\
s [1-7] : shuffle the cards in a run (cheat!)\n\
p [1-7] : put a hidden pile onto the run (cheat!)\n\
w [1-7] : print the cards in a hidden pile (cheat!)\n\
h or ? : print this command summary\n\
r : print the rules of the game\n\
q : quit\n\n");
(void) printf("\
Moving: There are three parameters in the m command: the first is the\n\
run to be moved from, the second is the card number to split\n\
the run at (check numbers along the top), and the third is\n\
the destination run (or 'a' if an ace pile). Since only one card\n\
can be moved to an ace at a time, the pos paramter is ignored\n\
in this case. To give a couple of examples 'm 6 1 4 would move\n\
all cards from run 6 to run 4, and m 3 5 7 would split run 3\n\
before the fifth card (leaving 4) and move the rest to run 7.\n\n\
Cheating: Commands that allow cheating are available but they will count\n\
against you in your next life!\n\n\nHit Return -");
(void) fflush(stdout);
(void) getchar();
redraw(TRUE);
}
disprules()
{
clear();
(void) printf("\
Object: The object of this game is to get all of the cards in each suit\n\
in order on the proper ace pile.\n\n\
Rules: Cards are played on the ace piles in ascending order: A,2,...,K.\n\
All aces are automatically placed in the correct aces pile as\n\
they're uncovered in runs or from a pile of hidden cards. Once a\n\
card is placed in an ace pile it can't be removed.\n\n");
(void) printf("\
When moving runs, they can be split anywhere, but the top card\n\
in the run moved must be placed on a card one higher (i.e. 5 on\n\
a 6) and always on a card of the opposite color.\n\n");
(void) printf("\
Whenever a whole run is moved, the top hidden card is turned\n\
over, thus becoming the beginning of a new run. If there are no\n\
hidden cards left, a space is created which can only be filled by\n\
a king.\n\n\nHit Return -");
(void) fflush(stdout);
(void) getchar();
redraw(TRUE);
}
getst(getbuf)
char *getbuf;
{
int ch;
int nc;
*(resgb = getbuf) = nc = 0;
for (;;)
{
if ((((ch = getchar()) >= ' ' && ch <= '~') || ch == '\t') && nc < 40)
{
*getbuf++ = ch;
*getbuf = 0;
nc++;
(void) putchar(ch);
(void) fflush(stdout);
}
else if (ch == '\b')
{
if (nc--)
{
*--getbuf = 0;
(void) printf("\b \b");
(void) fflush(stdout);
}
else
return(FALSE);
}
else if (ch == ('r' & 0x1f))
redraw(TRUE);
else if (ch == '\r')
return(TRUE);
}
}
shuffle(cards)
int *cards;
{
int i, j, k, t;
for (i = 0; cards[i] != -1; i++)
;
if (i)
{
for (j = 0; j < i; j++)
{
do
k = rnd(i);
while (k == j);
t = cards[j];
cards[j] = cards[k];
cards[k] = t;
}
}
}
rnd(n)
{
return(((rand() >> 12 | rand() >> 6) & (unsigned) ~0 >> 1) % n);
}
lstlen(list)
int *list;
{
int i;
for (i = 0; list[i] != -1; i++)
;
return(i);
}
xputc(c)
register c;
{
(void) putchar(c);
}
init()
{
printf("\033\143");
printf("\233H\233J");
set_raw();
}
deinit()
{
set_con();
}
clear()
{
printf("\233H\233J");
}
cleol()
{
printf("\233K");
}
standout()
{
putchar('\233');
printf("42m");
}
standend()
{
putchar('\233');
printf("40m");
}
moveto(y, x)
{
printf("\233%d;%dH",(y)+1,(x));
}
put(dest, item)
int *dest;
{
if (item == -1)
return;
while (*dest != -1)
dest++;
*dest++ = item;
*dest = -1;
}
get(source)
int *source;
{
int i;
for (i = 0; *source != -1; i++, source++)
;
if (i)
{
i = *--source;
*source = -1;
return(i);
}
return(-1);
}
peek(source)
int *source;
{
int i;
for (i = 0; source[i] != -1; i++)
;
return(i ? source[i - 1] : -1);
}
_abort()
{
(void) getchar();
unwind(0);
}